package com.zx.kitchen.index.common.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.zx.kitchen.index.R;
import com.zx.kitchen.index.common.entity.DishesInfoBean;
import com.zx.kitchen.index.common.entity.EditInfoBean;
import com.zx.kitchen.index.common.utils.ArithmeticUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/3/7.
 */

public class EditInfoAdapter extends BaseAdapter {
    private List<DishesInfoBean> objects = new ArrayList<DishesInfoBean>();

    private Context context;
    private LayoutInflater layoutInflater;


    String num = "0";
    String price = "0";
    private int index = 0;

    public EditInfoAdapter(Context context, List<DishesInfoBean> objects) {
        this.context = context;
        this.objects = objects;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public DishesInfoBean getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.adapter_edit_info, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((DishesInfoBean) getItem(position), (ViewHolder) convertView.getTag(), position);
        return convertView;
    }

    private void initializeViews(final DishesInfoBean object, final ViewHolder holder, final int position) {
        holder.dishesName.setText(object.getName());
        holder.dishesStandard.setText(object.getStandard());
        holder.etPrice.setText(object.getPrice());
        holder.tvUnit.setText("元/" + object.getUnit());
        holder.etNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (!TextUtils.isEmpty(s.toString())) {
                    num = s.toString();
                }
                if (!TextUtils.isEmpty(holder.etPrice.getText().toString())) {
                    price = holder.etPrice.getText().toString();
                }
                onEditListener.onEdit(num, price, position);
            }
        });

        holder.etPrice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (!TextUtils.isEmpty(s.toString())) {
                    price = s.toString();
                }
                if (!TextUtils.isEmpty(holder.etNum.getText().toString())) {
                    num = holder.etNum.getText().toString();
                }
                onEditListener.onEdit(num, price, position);

            }
        });
        holder.ivDeleteItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onEditListener.onDelete(v, position);
            }
        });

        holder.etNum.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    index = position;
                }
                return false;
            }
        });
        holder.etNum.clearFocus();

        if (index != -1 && index == position) {
            // 如果当前的行下标和点击事件中保存的index一致，手动为EditText设置焦点。
            holder.etNum.requestFocus();
            holder.etNum.setSelection(holder.etNum.getText().length());
        }
        holder.etPrice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.etPrice.setText(holder.etPrice.getText().toString());
                holder.etPrice.selectAll();
            }
        });
    }


    private OnEditListener onEditListener = null;

    public static interface OnEditListener {
        void onEdit(String num, String price, int position);

        void onDelete(View view, int position);
    }

    public void setOnItemClickListener(OnEditListener listener) {
        this.onEditListener = listener;
    }

    protected class ViewHolder {
        private ImageView ivDeleteItem;
        private TextView dishesName;
        private TextView dishesStandard;
        private EditText etNum;
        private EditText etPrice;
        private TextView tvUnit;

        public ViewHolder(View view) {
            ivDeleteItem = (ImageView) view.findViewById(R.id.iv_delete_item);
            dishesName = (TextView) view.findViewById(R.id.dishes_name);
            dishesStandard = (TextView) view.findViewById(R.id.dishes_standard);
            etNum = (EditText) view.findViewById(R.id.et_num);
            etPrice = (EditText) view.findViewById(R.id.et_price);
            tvUnit = (TextView) view.findViewById(R.id.tv_unit);
        }
    }
}
